package com.example.zzl.view.activity.SearchActivity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.adapter.SearchFragment.GvCataAdapter;
import com.example.zzl.databinding.ActivityCategoryBinding;
import com.example.zzl.model.bean.SearchFragment.CateData;
import com.example.zzl.viewmodel.SearchFragment.Classify;
import com.example.zzl.viewmodel.SearchFragment.ResultData;

import java.util.ArrayList;

public class CategoryActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener{

    private ActivityCategoryBinding binding;
    private TextView tv;
    private GridView gv;
    private ArrayList<CateData.ResultBean> list;
    private GvCataAdapter adapter;
    private Classify classify;
    private Intent intent;
    private String url,title;
    public static Handler handler;
    public static ResultData resultData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        binding = DataBindingUtil.setContentView(this,R.layout.activity_category);
        resultData = new ResultData();
        init();
    }

    private void init() {
        intent = getIntent();
        tv = binding.tvCateTitle;
        gv = binding.gvCate;
        gv.setOnItemClickListener(this);
        list = new ArrayList<>();

        url = intent.getStringExtra("url");
        title = intent.getStringExtra("title");
        tv.setText(title);
        binding.setMyClick(this);
        loadData();
    }

    private void loadData(){
        classify = new Classify(url,"Cate");
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 0x301){
                    list.add(new CateData.ResultBean());
                    for (int i = 0; i < resultData.cates.size(); i++) {
                        list.add(resultData.cates.get(i));
                    }
                    adapter = new GvCataAdapter(CategoryActivity.this,list);
                    gv.setAdapter(adapter);
                }
            }
        };
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                //Toast.makeText(this, "54343434", Toast.LENGTH_SHORT).show();
                setResult(100, intent);
                finish();
                break;

        }
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        Intent intent = new Intent(this,SearchResultActivity.class);
        //Log.e("data",i+"");
        if(i==0){
            intent.putExtra("search",title);
        }else {
            intent.putExtra("search",list.get(i).getCatName());
        }
        intent.putExtra("cate",list.get(i).getCatUrl());
        intent.putExtra("type","cateActivity");
        startActivityForResult(intent,600);
    }
}
